package com.krtech.wecard.module.mayi.task;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.config.WecardRedisTemplate;
import com.krtech.wecard.module.mayi.MayiPayEnums;
import com.krtech.wecard.module.mayi.entity.MayiCommentLog;
import com.krtech.wecard.module.mayi.entity.MayiDianzanCount;
import com.krtech.wecard.module.mayi.entity.MayiDianzanLog;
import com.krtech.wecard.module.mayi.entity.MayiPayList;
import com.krtech.wecard.module.mayi.mapper.MayiDianzanCountMapper;
import com.krtech.wecard.module.mayi.mapper.MayiPayListMapper;
import com.krtech.wecard.module.mayi.service.MayiCommentLogService;
import com.krtech.wecard.module.mayi.service.MayiDianzanCountService;
import com.krtech.wecard.module.mayi.service.MayiDianzanLogService;
import com.krtech.wecard.module.mayi.service.MayiPayListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class MayiDianzanRedisFixTask {

    @Autowired
    private MayiPayListMapper payListMapper;

    @Autowired
    private MayiDianzanLogService dianzanLogService;

    @Autowired
    private MayiDianzanCountMapper dianzanCountMapper;

    @Autowired
    private MayiCommentLogService commentLogService;

    @Autowired
    private WecardRedisTemplate redisTemplate;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void userTask(){
        //30s修复一次redis,只解决正在进行中，但是未达标的
        List<MayiPayList> onlineComment = payListMapper.getOnlineComment();

        for (MayiPayList comment:onlineComment){
            if (StrUtil.isNotEmpty(comment.getType()) && comment.getType().equals(MayiPayEnums.TYPE_REVIEW)) {
                //查询redis的数量
                int commentCount = commentLogService.count(new QueryWrapper<MayiCommentLog>().eq(MayiCommentLog.COL_FK_PAY_LIST_ID, comment.getId()));

                //如果小于0，就判断是否数量达标，如果没有达标，就进行数量修复
                if (commentCount < comment.getRealGoalCount()){
                    Integer repairCount = comment.getRealGoalCount() - commentCount;
                    redisTemplate.set("comment:"+comment.getId(),repairCount.toString());
                }
            }
        }


        List<MayiPayList> onlineDianzan = payListMapper.getOnlineDianzan();

        for (MayiPayList dianzan:onlineDianzan){
            //判断数量是否已经达标了
            int dianzanCount = dianzanLogService.count(new QueryWrapper<MayiDianzanLog>().eq(MayiDianzanLog.COL_FK_PAY_LIST_ID, dianzan.getId()));
            if (dianzanCount >= dianzan.getGoalCount()){
                //说明结束了
                //判断是否有结束时间
                if (dianzan.getEndTime() == null){
                    dianzan.setEndTime(new Date());
                }
                dianzan.setStatus(MayiPayEnums.PAY_AND_RUN_OVER);
                payListMapper.updateById(dianzan);
            }
        }
    }
}
